package me.helllp.book.netty.step02;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class FutureExample {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		ExecutorService executor = Executors.newCachedThreadPool();
		
		Runnable task1 = new Runnable() {
			@Override
			public void run() {
				System.out.println("这是任务1。。。");				
			}
		};
		
		Callable<Integer> task2 = new Callable<Integer>() {
			
			@Override
			public Integer call() throws Exception {
				return new Integer(100);
			}
		};
		
		Future<?> f1 = executor.submit(task1);
		Future<Integer> f2 = executor.submit(task2);
		
		System.out.println("task1 is computed..? " + f1.isDone());
		System.out.println("task2 is computed..? " + f2.isDone());
		
		while(f1.isDone()){
			System.out.println("任务1执行结束");
			break;
		}
		
		while(f2.isDone()){
			System.out.println("任务2执行结束, 结果=" + f2.get());
			break;
		}
	}
}
